Replace create() classmethods with __init__ constructors#310
Replace create() classmethods with __init__ constructors#310NodeJSmith merged 4 commits intomainfrom
Conversation
The Resource class hierarchy used @classmethod create() factories that instantiated the class, set attributes on the instance, and returned it. This indirection required inspect.signature introspection in add_child() to conditionally pass arguments — fragile and hard to follow. Replace all create() classmethods with standard __init__ constructors that call super().__init__() and set attributes directly. Simplify add_child() to a direct constructor call. Remove the inspect import and the base Resource.create() method entirely. 25 files, -81 lines net. All 1053 tests pass, pyright clean.
There was a problem hiding this comment.
Pull request overview
This PR removes the @classmethod create() factory pattern from Hassette Resource subclasses and shifts construction to standard __init__ constructors, simplifying child creation and aligning tests/types with the new instantiation style.
Changes:
- Removed
Resource.create()and simplifiedResource.add_child()to directly instantiate child resources via constructors. - Converted multiple
Resource/Servicesubclasses fromcreate()factories to__init__()initialization. - Updated state manager typing stub and test call sites to use direct construction instead of
.create().
Reviewed changes
Copilot reviewed 26 out of 26 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/hassette/resources/base.py |
Removes Resource.create() and changes add_child() to call constructors directly. |
src/hassette/task_bucket/task_bucket.py |
Replaces TaskBucket.create() with __init__() initialization and readiness marking. |
src/hassette/state_manager/state_manager.py |
Adds StateManager.__init__() and removes the previous create() factory. |
src/hassette/state_manager/state_manager.pyi |
Updates stub to reflect constructor-based initialization. |
src/hassette/scheduler/scheduler.py |
Converts Scheduler.create() factory into __init__(). |
src/hassette/core/websocket_service.py |
Converts WebsocketService.create() factory into __init__(). |
src/hassette/core/web_api_service.py |
Converts WebApiService.create() factory into __init__(). |
src/hassette/core/state_proxy.py |
Converts StateProxy.create() factory into __init__() wiring of children/state. |
src/hassette/core/service_watcher.py |
Converts ServiceWatcher.create() factory into __init__(). |
src/hassette/core/scheduler_service.py |
Converts SchedulerService.create() and _ScheduledJobQueue.create() into __init__(). |
src/hassette/core/data_sync_service.py |
Converts DataSyncService.create() factory into __init__(). |
src/hassette/core/bus_service.py |
Converts BusService.create() factory into __init__() (including stream kw-only). |
src/hassette/core/app_handler.py |
Converts AppHandler.create() factory into __init__()-based wiring. |
src/hassette/core/app_factory.py |
Updates app instantiation from app_class.create(...) to app_class(...). |
src/hassette/core/api_resource.py |
Converts ApiResource.create() factory into __init__(). |
src/hassette/core/core.py |
Updates Hassette construction to use TaskBucket(...) instead of TaskBucket.create(...). |
src/hassette/bus/bus.py |
Converts Bus.create() factory into __init__() while preserving behavior. |
src/hassette/api/api.py |
Converts Api.create() factory into __init__() and updates sync facade child creation. |
src/hassette/api/sync.py |
Replaces ApiSyncFacade.create() with __init__() (note: file is marked auto-generated). |
src/hassette/app/app.py |
Converts App.create() factory into __init__() wiring of child resources. |
src/hassette/test_utils/harness.py |
Updates harness to instantiate TaskBucket(...) directly. |
tests/unit/test_app_factory.py |
Updates mocks/assertions for constructor calls rather than .create(). |
tests/integration/test_websocket_service.py |
Updates fixture to construct WebsocketService(...) directly. |
tests/integration/test_states.py |
Updates StateManager construction from .create() to direct instantiation. |
tests/integration/test_state_proxy.py |
Updates StateProxy construction from .create() to direct instantiation. |
tests/integration/test_service_watcher.py |
Updates ServiceWatcher construction from .create() to direct instantiation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Applies the same create()-to-__init__() conversion to DatabaseService, which was added on main in #305 after this branch diverged.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 30 out of 30 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #310 +/- ##
==========================================
- Coverage 80.07% 79.99% -0.09%
==========================================
Files 134 134
Lines 9586 9531 -55
Branches 948 943 -5
==========================================
- Hits 7676 7624 -52
Misses 1538 1538
+ Partials 372 369 -3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
@classmethod create()factory pattern with standard__init__()constructors across all 17 Resource subclasses, eliminating theinspect.signatureindirection inadd_child()and the baseResource.create()methodadd_child()from introspecting the child'screate()signature to conditionally pass args, to a directchild_class(hassette=self.hassette, parent=self, **kwargs)callStateManagertype stub and all test call sites to match the new construction pattern25 files changed, -82 lines net. All 1053 tests pass, pyright clean.